/*
 * GameSelectView.cpp
 *
 *  Created on: 01/06/2014
 *      Author: javier
 */

#include "GameSelectView.h"
#include "BZPView.h"
#include "Utils.h"

GameSelectView::GameSelectView(BZPView* view, SDLWindow* window) {
	this->view = view;
	this->window = window;
	std::string resDir = properties.getProperty("res_dir");
	std::string textureName = resDir + "game_select_bg.jpg";
	texture = window->loadTexture(textureName);
	geometry.x = 0;
	geometry.y = 0;
	geometry.w = properties.getPropertyAsInt("screen.width");
	geometry.h = properties.getPropertyAsInt("screen.height");
	showCampaign = false;
	// carga de imagenes de campanias
	std::string previewsDir = properties.getProperty("previews_dir");
	std::string previewsFiles = properties.getProperty("previewsFiles");
	std::vector<std::string> names = Utils::split(previewsFiles, ',');
	std::vector<std::string>::iterator it;
	for (it = names.begin(); it != names.end(); ++it) {
		textureName = previewsDir + *it;
		SDL_Texture* t = window->loadTexture(textureName);
		textures[*it] = t;
	}
	textureName = previewsDir + "notFound.jpg";
	notFound = window->loadTexture(textureName);
}

void GameSelectView::handleClick(int x, int y) {
	unsigned int pos = (y - 70) / 50;
	if (showCampaign) {
		if (pos < campaigns.size()) {
			std::string id = campaigns[pos]->getId();
			// TODO pedir el nombre de la campania por SDL
			Properties p;
			std::string name = p.getProperty("new.name.match");
			view->sendNewCampaign(id, name);
		}
	} else {
		if (pos < partidas.size()) {
			std::string id = partidas[pos]->getId();
			view->sendNewMatch(id);
		}
	}
}

void GameSelectView::render() {
	if (showCampaign) {
		window->renderTexture(texture, &geometry);
		SDL_Color color = { 255, 255, 255 };
		window->renderTexture(
				window->renderText(std::string("Elegir campania"), color), 20,
				20);
		std::vector<Match*>::iterator iter;
		int y = 70;
		std::string previewsFiles = properties.getProperty("previewsFiles");
		std::vector<std::string> names = Utils::split(previewsFiles, ',');
		std::vector<std::string>::iterator it;
		for (iter = campaigns.begin(); iter != campaigns.end(); ++iter) {
			Match* m = *iter;
			std::string name = m->getImage();
			SDL_Rect geometry;
			geometry.x = 10;
			geometry.y = y;
			geometry.w = 30;
			geometry.h = 30;
			bool findName = false;
			std::string nameFounded;
			for (it = names.begin(); it != names.end(); ++it) {
				if (name.compare(*it) == 0) {
					findName = true;
					nameFounded = *it;
				}
			}
			if (!findName) {
				window->renderTexture(notFound, &geometry);
			} else{
				window->renderTexture(textures[name], &geometry);
			}
			window->renderTexture(
					window->renderText(m->getId() + ": " + m->getName(), color),
					55, y);
			y += 50;
		}
	} else {
		window->renderTexture(texture, &geometry);
		SDL_Color color = { 255, 255, 255 };
		window->renderTexture(
				window->renderText(std::string("Elegir Partida"), color), 20,
				20);
		std::vector<Match*>::iterator iter;
		int y = 70;
		for (iter = partidas.begin(); iter != partidas.end(); ++iter) {
			Match* m = *iter;
			window->renderTexture(
					window->renderText(
							m->getId() + " | " + m->getName() + " | "
									+ m->getNombreCampania(), color), 20, y);
			y += 50;
		}
	}
}

void GameSelectView::showCampaigns() {
	showCampaign = true;
}

void GameSelectView::showMatchs() {
	showCampaign = false;
}

GameSelectView::~GameSelectView() {
	SDL_DestroyTexture(texture);
	std::map<std::string, SDL_Texture *>::iterator it;
	for (it=textures.begin();it!=textures.end();++it){
		SDL_DestroyTexture(it->second);
	}
}

void GameSelectView::loadCampaigns(std::string strCampaigns) {
	campaigns.clear();
	std::vector<std::string> splits = Utils::split(strCampaigns, '|');
	std::vector<std::string>::iterator iter;
	for (iter = splits.begin(); iter != splits.end(); ++iter) {
		std::vector<std::string> tokens = Utils::split(*iter, '\t');
		Match* m = new Match();
		m->setId(tokens[0]);
		m->setName(tokens[1]);
		m->setImage(tokens[2]);
		campaigns.push_back(m);
	}
}

void GameSelectView::loadMatchs(std::string matchs) {
	partidas.clear();
	std::vector<std::string> splits = Utils::split(matchs, '|');
	std::vector<std::string>::iterator iter;
	for (iter = splits.begin(); iter != splits.end(); ++iter) {
		std::vector<std::string> tokens = Utils::split(*iter, '\t');
		Match* m = new Match();
		m->setId(tokens[0]);
		m->setName(tokens[1]);
		m->setIdCampania(tokens[2]);
		if (tokens.size() > 3)
			m->setNombreCampania(tokens[3]);
		else
			m->setNombreCampania("(sin nombre)");
		partidas.push_back(m);
	}
}
